﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.ScheduleJob.Dao;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Manager
{
    /// <summary>
    /// 运行时计划任务实例管理
    /// </summary>
    public class ScheduleJobInstanceManager
    {
        private static readonly object lockObj = new object();
        private static ScheduleJobInstanceManager _manager = null;
        private ScheduleJobRTPLogic logic = null;
        private ScheduleJobInstanceManager()
        {
            logic = new ScheduleJobRTPLogic();
        }
        public static ScheduleJobInstanceManager GetInstance()
        {
            if (_manager == null)
            {
                lock (lockObj)
                {
                    if (_manager == null)
                        _manager = new ScheduleJobInstanceManager();
                }
            }
            return _manager;
        }
        public bool IsExistsInstance(string instanceID)
        {
            return logic.IsExistsInstance(instanceID);
        }
        public void SaveScheduleJobInstance(ScheduleJobInstanceStore instance)
        {
            var isExistsInstance = logic.IsExistsInstance(instance.InstanceID);
            if (!isExistsInstance)
                logic.AddScheduleJobInstance(instance);
            else if (instance.ScheduleJobExecuteState == JobExecuteState.Publish_Delete)
            {
                ScheduleJobStateNoticeData data = new ScheduleJobStateNoticeData();
                data.JobState = JobExecuteState.Deleted;
                data.InstanceID = instance.InstanceID;
                UpdateScheduleJobInstanceState(instance.InstanceID,data);
            }
        }
        public List<ScheduleJobInstanceStore> GetScheduleJobInstances(UserDefineQueryFilter filter)
        {
            var list = logic.GetScheduleJobInstances(filter);
            return list;
        }
        public void PublishScheduleJob(ScheduleJobInstanceStore store)
        {
            SaveScheduleJobInstance(store);
        }
        public List<ScheduleJobInstanceStore> GetAllToBeExecutedScheduleJobInstances()
        {
            var list = logic.GetAllToBeExecutedScheduleJobInstances();
            return list;
        }
        public List<ScheduleJobInstanceStore> GetAllNeedAutoStartScheduleJobInstances()
        {
            var list = logic.GetAllNeedAutoStartScheduleJobInstances();
            return list;
        }
        public ScheduleJobInstanceStore GetScheduleJobInstanceByInstanceID(string instanceID)
        {
            return logic.GetScheduleJobInstanceByInstanceID(instanceID);
        }
        public void UpdateScheduleJobInstanceState(string instanceID, ScheduleJobStateNoticeData data)
        {
            logic.UpdateScheduleJobInstanceState(instanceID, data);
        }
        public List<ScheduleJobInstanceStore> GetReadyToFireScheduleJobInstances()
        {
            UserDefineQueryFilter filter = new UserDefineQueryFilter();
            filter.Add("ScheduleJobState", JobExecuteState.New);
            return logic.GetReadyToFireScheduleJobInstances(filter);
        }
    }
}
